package com.yyryyty.chapter5;

import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.joining;

/**
 * @Description : Practise Makes Perfect !
 * @Author : YTY
 * @Date : 2021-01-17 17:43
 */
public class PuttingIntoPractise {

    public static void main(String[] args) {

        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        // 1.
//        System.out.println(transactions.stream()
//                .filter(transaction -> transaction.getYear() == 2011)
//                .sorted(comparing(Transaction::getValue))
//                .count());

        // 2.
//        transactions.stream()
//                .map(Transaction::getTrader)
//                .map(Trader::getCity)
//                .collect(toSet())
//                .forEach(System.out::println);

        // 3.
//        transactions.stream()
//                .map(Transaction::getTrader)
//                .filter(trader -> trader.getCity().equals("Cambridge"))
//                .distinct()
//                .sorted(comparing(Trader::getName).reversed())
//                .forEach(System.out::println);

        // 4.
//        System.out.println(transactions.stream()
//                .map(transaction -> transaction.getTrader().getName())
//                .distinct()
//                .sorted()
//                .reduce("", (n1, n2) -> n1 + n2));

        System.out.println(transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .collect(joining()));

        // 5.
//        System.out.println(transactions.stream()
//                .map(Transaction::getTrader)
//                .map(Trader::getCity)
//                .anyMatch(city -> city.equals("Milan")));

//        System.out.println(transactions.stream()
//                .anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan")));

        // 6.
//        System.out.println(transactions.stream()
//                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
//                .map(Transaction::getValue)
//                .reduce(Integer::sum).get());

        // 7.
//        System.out.println(transactions.stream()
//                .map(Transaction::getValue)
//                .max(Integer::compareTo).get());

//        transactions.stream()
//                .map(Transaction::getValue)
//                .reduce(Integer::max)
//                .ifPresent(System.out::println);

        // 8.
//        transactions.stream()
//                .map(Transaction::getValue)
//                .reduce(Integer::min)
//                .ifPresent(System.out::println);

//        System.out.println(transactions.stream()
//                .mapToInt(Transaction::getValue)
//                .min().getAsInt());

    }
}
